import pygame.event
from pygame.sprite import Group

import global_var
from game_menu.pause_menu import PauseMenu
from game_menu.replay_menu import ReplayMenu
from settings import Settings
from game_functions.collision import check_collision
from game_functions.events import check_events, begin_start_screen
from game_functions.render import *
from game_objects.ship import Ship
from game_screens.start_screen import StartScreen


class Game:

    running = False # 定义一个全局变量running，用于控制游戏主循环

    def __init__(self) -> None:
        # self.ship_class = Ship
        # 更加灵活的一种写法，只是将类名作为参数传递，而不是实例化对象
        # 通过Game类的属性来创建Ship实例优点如下：
        # 可以实现更换Ship类的时候不用修改Game类中run_game方法，只需修改Game类的__init__方法即可
        self.settings = Settings()
        self.clock = pygame.time.Clock()
        self.screen = config_screen(self.settings)
        self.ship_class = Ship
        self.bullets_class = Group
        self.aliens_class = Group
        self.replay_menu_class = ReplayMenu
        self.pause_menu_class = PauseMenu


    def run_game(self) -> None:
        ship, bullets_group, aliens_group, replay_menu, pause_menu, start_screen = self.ins_object()
        begin_start_screen(ship, start_screen)  # 显示开始屏幕

        # 游戏主循环
        while global_var.get_value('running'):
            match global_var.get_game_state():  # 根据游戏状态执行不同的操作
                case 'living':
                    # 游戏运行中
                    check_events(ship,start_screen)
                    ship.update()
                    if global_var.get_value('sustain_fire'):
                        bullet_fire(bullets_group, self.screen, self.settings, ship)
                    bullets_group.update()
                    aliens_group.update()
                    check_collision(bullets_group, aliens_group)
                    if check_collision(ship, aliens_group):
                        # 弹出结束游戏菜单
                        print('Game Over Right Now ?')
                        replay_menu.replay_game_menu.mainloop(self.screen)

                    update_screen(self.settings, self.screen, ship, bullets_group, aliens_group)
                    self.clock.tick(60)

                case 'paused':
                    # 游戏暂停
                    check_events(ship,start_screen)
                    pause_menu.pause_game_menu.update(pygame.event.get())
                    pause_menu.pause_game_menu.draw(self.screen)
                    pygame.display.update()
                    self.clock.tick(60)
                    print('Game Paused')


    def ins_object(self):
        start_screen = StartScreen(self.screen, self.settings)  # 创建开始屏幕对象
        ship = self.ship_class(self.screen, self.settings)  # 在此处才真正的实例化Ship对象
        bullets_group = self.bullets_class()  # 在此处才真正的实例化bullets Group对象
        aliens_group = self.aliens_class()  # 存放外星人的编组此处真正的实例化aliens Group对象
        create_fleet(self.screen, self.settings, aliens_group)  # 创建外星人飞船舰队
        replay_menu = self.replay_menu_class(self.settings, self, bullets_group)  # 创建菜单实例化对象
        pause_menu = self.pause_menu_class(self.settings)  # 创建菜单实例化对象
        return ship, bullets_group, aliens_group, replay_menu, pause_menu, start_screen


if __name__ == "__main__":
    """
    程序入口
    """
    game = Game()
    game.run_game()